package com.zidian.leader.activity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.widget.Button;

import com.jakewharton.rxbinding.view.RxView;
import com.orhanobut.logger.Logger;
import com.zidian.leader.R;
import com.zidian.leader.api.HttpFactory;
import com.zidian.leader.common.activity.BaseActivity;
import com.zidian.leader.common.util.HawkUtils;
import com.zidian.leader.common.util.StringUtils;
import com.zidian.leader.common.view.ClearEditText;
import com.zidian.leader.entity.LoginResult;

import java.util.concurrent.TimeUnit;

import butterknife.Bind;
import butterknife.OnClick;
import rx.Subscriber;
import rx.functions.Action1;

/**
 * 登录
 * Created by GongCheng on 2016/11/2.
 */

public class LoginActivity extends BaseActivity {
    @Bind(R.id.username_et)
    ClearEditText usernameEt;
    @Bind(R.id.password_et)
    ClearEditText passwordEt;
    @Bind(R.id.commit_bt)
    Button commitBt;

    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }



    @Override
    protected int getLayout() {
        return R.layout.activity_login;
    }

    /**
     * 初始化界面
     */
    private void initView() {
        usernameEt.setText(HawkUtils.getUsername());
        //RxBing 点击事件，防止重复点击
        RxView.clicks(commitBt)
                .throttleFirst(2000, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        onClick();
                    }
                });
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("正在登录...");
        usernameEt.setText(HawkUtils.getUsername());
    }
    /**
     * 登录按钮点击事件
     */
    @OnClick(R.id.commit_bt)
    public void onClick() {
        final String username = usernameEt.getText().toString().trim();
        final String password = passwordEt.getText().toString().trim();
        if (!StringUtils.hasText(username)) {
            Snackbar.make(usernameEt, "请输入用户名", Snackbar.LENGTH_SHORT).show();
            return;
        }
        if (!StringUtils.hasText(password)) {
            Snackbar.make(usernameEt, "请输入密码", Snackbar.LENGTH_SHORT).show();
            return;
        }

        subscription = HttpFactory.getInstance().login(username, password).subscribe(new Subscriber<LoginResult>() {
            @Override
            public void onStart() {
                super.onStart();
                progressDialog.show();
            }

            @Override
            public void onCompleted() {
                progressDialog.dismiss();
            }

            @Override
            public void onError(Throwable e) {
                Logger.e(e.toString());
                Snackbar.make(usernameEt, e.getMessage(), Snackbar.LENGTH_SHORT).show();
                progressDialog.dismiss();
            }

            @Override
            public void onNext(LoginResult loginResult) {
                Logger.i(loginResult.toString());
                Logger.i(loginResult.getData().toString());
                Logger.i(loginResult.getData().getCollegeList().toString());
                HawkUtils.setUsername(username);
                HawkUtils.setPassword(password);
                HawkUtils.setRoleId(loginResult.getRoleId());
                HawkUtils.setToken(loginResult.getToken());
                HawkUtils.setColleges(loginResult.getData().getCollegeList());
                HawkUtils.setIsLogin(true);
                HawkUtils.setLeadName(loginResult.getLeadName());
                startActivity(MainActivity.class);
            }
        });
    }
}
